package com.game.utils
{
	import com.greensock.TweenLite;
	
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.DisplayObject;
	import flash.events.Event;
	
	public class MenuFade
	{
		
		private var _disObj:DisplayObject;
		private var _isBm:Boolean;
		private var _addDis:uint=15;
		private var _bmd:BitmapData;
		private var _bm:Bitmap;
		public function MenuFade(_disObj1:DisplayObject,isBm:Boolean=true)
		{
			_disObj=_disObj1;
			_isBm=isBm;
			
		}
		public function gc():void{
			_disObj=null;
			clearBm();
		}
		private function clearBm():void{
			if(_bmd!=null){
				_bmd.dispose();
			}
			if(_bm!=null&&_bm.parent){
				_bm.parent.removeChild(_bm)
			}
			_bmd=null;
			_bm=null;
		}
		public function fadeIn(yNum:int,fun:Function=null):void{
			getBm(_disObj);
			var _thisDis:DisplayObject;
			var _maxY:int=yNum+_addDis;
			var _type:uint=0;
			if(_isBm){
				_thisDis=_bm;
			}else{
				_thisDis=_disObj;
			}
			
			
			new TweenLite(_thisDis,0.3,{y:_maxY,onComplete:function():void{
				
				new TweenLite(_thisDis,0.1,{y:yNum,onComplete:function():void{
					clearBm();
					_disObj.visible=true;
					_disObj.y=yNum;
					if(fun!=null){
						fun();
					}
					fun=null;
				}
				});
				
				
			}
			});
		}
		public function fadeOut(yNum:int,fun:Function=null):void{
			getBm(_disObj);
			var _thisDis:DisplayObject;
			var _maxY:int=_disObj.y+_addDis;
			var _type:uint=0;
			
			if(_isBm){
				_thisDis=_bm;
			}else{
				_thisDis=_disObj;
			}
			new TweenLite(_thisDis,0.1,{y:_maxY,onComplete:function():void{
				new TweenLite(_thisDis,0.3,{y:yNum,onComplete:function():void{
					
					clearBm();
					_disObj.visible=true;
					_disObj.y=yNum;
					if(fun!=null){
						fun();
					}
					fun=null;
				}
				});
			}
			});
			
		}
		private function getBm(disObj:DisplayObject):void{
			if(!_isBm){
				return;
			}
			if(_bmd==null){
				_bmd = new BitmapData(disObj.width, disObj.height, true, 0);
				_bmd.draw(disObj);
			}
			if(_bm==null){
				_bm=new Bitmap(_bmd);
			}
			_bm.x=_disObj.x;
			_bm.y=_disObj.y;
			if(_disObj.parent){
				_disObj.parent.addChild(_bm);
			}
			_disObj.visible=false;
		}
		
	}
}